<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentInvite.class.php, $Author:  $ */



/** Component for invite group
 * @version $Id: PhpFox_ComponentInvite.class.php
 *  Max Kovrigovich
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentInvite extends BaseComponent
{
    /** Processing method
     */

    function process()
    {
    	App::membersOnly();

        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oSecurityService->checkPerm('groups', array('read_only', 'full'));

        $oGroupsService = $this->_oModule->getService('Groups');
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */

        $oFriendsService = &App::getModuleService('Account', 'Friends');
        /* @var $oFriendsService PhpFox_Mod_Account_ServiceFriends */
        $oMessengerService = &App::getModuleService('Messenger', 'Messenger');

        $oTpl = $this->_oModule->getTemplate();

        App::loadClass('Pager');
        App::loadClass('Validator');

        $oReq = &App::getRequest();
        /* @var $oReq Request */

        $oUrl = &App::getUrl();
        /* @var $oUrl Url */

        $isEmailErrors = false;

        $oUser = $this->getParam('user');

        $iGroupId = $oReq->getInt('id', 0);
        $iPageSize = App::getParam('public.pagesize');
        $iPage = $oReq->getInt('page', 1);

        $bIsMember = $oGroupsService->isMember($iGroupId, $oUser->aData['user']);
        $bIsOwner = $oGroupsService->isOwner($iGroupId, $oUser->aData['user']);

        $aGroupData = $oGroupsService->getGroupData($iGroupId);

        $bIsFreeInvite = (isset($aGroupData['mem_invite']) and $aGroupData['mem_invite'] == 'Yes');

        list($aFriends, $iCnt) = $oFriendsService->getFriendsList($oUser, $iPage, $iPageSize);

        $aFriends = $oGroupsService->removeInvited($iGroupId, $aFriends);

        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

        $aPages = array();
        for($i = 0; $i < $oPager->nPagesCount; $i++)
            $aPages[] = $i + 1;

         $aInviteMembers = array();
         //process invite
         if($oReq->getAction() == 'invite')
         {
             $isEmailErrors = false;
	         $aInviteMembers = $oReq->getArray('listRight');
	         //remove invited
	         $aTmp = array();
	         foreach($aInviteMembers as $sUser)
	             $aTmp[] = array('user'=>$sUser);
	         $aFriends = $oGroupsService->removeInvited($iGroupId, $aTmp);

	         $iUserCnt = count($aInviteMembers);

	         $sInviteMails = $oReq->get('emails','');
	         $aInviteMails = explode(',', $sInviteMails);
	         $iEmailCnt = count($aInviteMails);

	         foreach($aInviteMembers as $sMember)
	         {
				//add member to group
				$oGroupsService->addInvite($oUser->aData['user'], $sMember, $iGroupId, $aGroupData['type']);

				//send notification
				$sInviteText=App::format('Mod_Groups.mail_user_invited_to_group', array(App::makeUrl('public.groups.view', array('id' => $iGroupId)), $aGroupData['title']));
				$oMessengerService->sendMessage(array('to'=>$sMember, 'title'=>App::format('Mod_Groups.title.invite'),'text'=>$sInviteText), $oUser->aData['user'], 'mail');
	         }

	         $oMailer = &App::newObject('mail_Mailer');
	         $aFields = array('email'        => array('title' => 'email', 'def' => 'email'));
	         $oValidator = &new Validator($aFields, array());

	         foreach($aInviteMails as $sEmail)
	         {
	            if($oValidator->isValid(array('email'=>$sEmail)))
	            {
	                $sUrl = App::makeUrl('groups.view', array('id'=>$iGroupId));
	        		$oMailer->send('email_group_invite', $sEmail, 'Group Invite',
	        		                array('USERNAME'=>$oUser->aData['user'],
	        		                      'SITENAME'=>App::getSetting('site_name'),
	        		                      'HOME'=>$sUrl));
	            } else
	            {
	                $iEmailCnt--;
	                $isEmailErrors = true;
	            }
	         }
         }

         $oTpl->assign(array(
            'isEmailErrors' => $isEmailErrors,
            'aInviteMembers' =>$aInviteMembers,
            'iUserCnt' => (isset($iUserCnt) ? $iUserCnt : '' ),
            'aInviteMails' => (isset($aInviteMails) ? $aInviteMails : ''),
            'iEmailCnt' => (isset($iEmailCnt) ? $iEmailCnt : ''),
            'aFriends' => $aFriends,
            'aPages' => $aPages,
            'iFrom'  => ($iCnt) ? ($iFrom + 1) : 0,
            'iTo'  => $iTo,
            'iCnt' => $iCnt,
            'iPageCnt' =>$oPager->nPagesCount,
            'iProcessing' => ($oReq->getAction() == 'invite'),
            'aGroupData'=> $aGroupData,
            'isFreeInvite'=>$bIsFreeInvite,
            'isMember' => $bIsMember,
            'isClosed' => (isset($bIsClosed) ? $bIsClosed : ''),
            'isOwner'  => $bIsOwner,
            'sImageUrl' => App::getParam('user.pic.url'),
            'bAjax' => App::getParam('useAjax'),
            'isAdmin'	=> App::isStaff(),
        ));
        $sHtml = $oTpl->fetch('Invite.html');
        return $sHtml;
    }
}
?>